//
//  HSeeRecordViewController.m
//  NursingManages
//
//  Created by Zhen Huang on 17/5/26.
//  Copyright © 2017年 huangzhen. All rights reserved.
//

#import "HSeeRecordViewController.h"
#import "HUnUploadViewController.h"
#import "HDidUploadViewController.h"
#import "HQc_codeModel.h"
#import "ModelToJson.h"
#import "Model_Hr_Ns_Inspect_New.h"
#import "InitDbTableQuality.h"
#import "UploadPubQuality.h"
#import "HCommon.h"
#import "HNightQualiatyViewController.h"
#import <MBProgressHUD.h>
@interface HSeeRecordViewController ()

@property (nonatomic, strong) UISegmentedControl *segment;
@property (nonatomic, strong) UIView *Rootviews;
@property (nonatomic, assign) NSInteger isContrler;
@property (nonatomic, strong) HUnUploadViewController *HunUploadVC;
@property (nonatomic, strong) HUnUploadViewController *HDidUploadVC;
@property (nonatomic, copy) NSString *level_id;
@property (nonatomic, strong) NSDictionary *param;
@property (nonatomic, assign) NSInteger isHnight;
@property (nonatomic, assign) NSInteger menu_id;
@end

@implementation HSeeRecordViewController

- (instancetype)initWithLevel_ID:(NSString *)level_id
                           param:(NSDictionary *)param
                        isHnight:(NSInteger)isHnight
                         menu_id:(NSInteger)menu_id

{
    if (self) {
        self.level_id = level_id;
        self.param = param;
        self.isHnight = isHnight;
        self.menu_id = menu_id;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createSegmentController];
    [self.view addSubview:self.Rootviews];
    
    
}

- (void)createSegmentController {
    self.segment = [[UISegmentedControl alloc]initWithItems:@[@"未上传",@"已上传"]];
    self.segment.tintColor = RGB(69, 185, 124);
    self.segment.selectedSegmentIndex = 0;
    self.segment.frame = CGRectMake(10,5 + NavBarHeight, ScreenWidth-20, 30);
    [self.segment setContentOffset:CGSizeMake(1,0) forSegmentAtIndex:0];
    [self.segment setContentOffset:CGSizeMake(4,0) forSegmentAtIndex:1];
    [self Change:self.segment];
    
    if (self.isHnight == 1) {
        UIBarButtonItem *UploadButton = [[UIBarButtonItem alloc] initWithTitle:@"上传"
                                                                        style:UIBarButtonItemStylePlain
                                                                       target:self
                                                                       action:@selector(didUploadButttonClicked:)];
        UIBarButtonItem *NewButton = [[UIBarButtonItem alloc] initWithTitle:@"新增"
                                                                       style:UIBarButtonItemStylePlain
                                                                      target:self
                                                                      action:@selector(NewHight:)];
        NSArray *buttonArray = [[NSArray alloc]initWithObjects:UploadButton,NewButton, nil];
        self.navigationItem.rightBarButtonItems = buttonArray;
    }else {
        UIBarButtonItem *execbtn = [[UIBarButtonItem alloc] initWithTitle:@"上传" style:UIBarButtonItemStylePlain target:self action:@selector(didUploadButttonClicked:)];
        self.navigationItem.rightBarButtonItem = execbtn;
    }
    
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:[UIColor blackColor]};
    [self.segment setTitleTextAttributes:dic forState:UIControlStateNormal];
    [self.segment addTarget:self action:@selector(Change:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.segment];
}
- (void)Change:(UISegmentedControl *)seg {
    NSInteger index = seg.selectedSegmentIndex;
    self.isContrler = index;
    switch (index) {
        case 1:{
            [self addChildViewController:self.HDidUploadVC];
            [self.Rootviews addSubview:self.HDidUploadVC.view];

        }
            break;
        case 0:{
            [self addChildViewController:self.HunUploadVC];
            [self.Rootviews addSubview:self.HunUploadVC.view];

        }
            break;
        default:
            break;
    }
}
- (UIView *)Rootviews
{
    if (!_Rootviews) {
        _Rootviews = [[UIView alloc]initWithFrame:CGRectMake(0, self.segment.bottom+ 15, ScreenWidth, ScreenHeight - self.segment.bottom - 15)];
        _Rootviews.backgroundColor = [UIColor whiteColor];
        _Rootviews.userInteractionEnabled = YES;
    }
    return _Rootviews;
}

- (HUnUploadViewController *)HunUploadVC
{
    if (!_HunUploadVC) {
        _HunUploadVC = [[HUnUploadViewController alloc]initWithType:1 level_id:self.level_id param:self.param isHNight:self.isHnight];
        _HunUploadVC.view.height = self.Rootviews.height;
    }
    return _HunUploadVC;
}
- (HUnUploadViewController *)HDidUploadVC
{
    if (!_HDidUploadVC) {
        _HDidUploadVC = [[HUnUploadViewController alloc]initWithType:2 level_id:self.level_id param:self.param isHNight:self.isHnight];
        _HDidUploadVC.view.height = self.Rootviews.height;
    }
    return _HDidUploadVC;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)didUploadButttonClicked:(UIButton *)sender
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"是否确定上传?"
                                                    message:nil
                                                   delegate:nil
                                          cancelButtonTitle:@"取消"
                                          otherButtonTitles:@"确定", nil];
    [alert show];
    alert.tag = 200;
    alert.delegate = self;

}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag == 200) {
        switch (buttonIndex) {
            case 0:{
                return;
            }
                break;
            case 1:{
                
//                [HCommon popupMessage:@"正在上传"];
                InitDbTableQuality *initd = [[InitDbTableQuality alloc]init];
                
                NSMutableArray *array = [initd GetDetailIUnUploadTable];
                
                NSMutableArray *dataArray = [initd GetMainTableWithUpload_flag:0 typy_id:[self.param objectForKey:@"TYPE_ID" ]level_id:self.level_id];
                
                NSMutableArray *Inspect = [initd getInspextNewWithReslut_id:dataArray];
                
                NSString *jsonText = [ModelToJson toJSON:dataArray options:NSJSONWritingPrettyPrinted error:nil];
                NSString *jsonTextDetail = [ModelToJson toJSON:array options:NSJSONWritingPrettyPrinted error:nil];
                NSString *jsonInspect = [ModelToJson toJSON:Inspect options:NSJSONWritingPrettyPrinted error:nil];
                
                NSLog(@"zhu:%@  mingxi:%@",jsonText,jsonTextDetail);
                
                UploadPubQuality *upload = [[UploadPubQuality alloc]init];
                                    
                    [upload startUpload:jsonText inspect_detail:jsonTextDetail inspectNew:jsonInspect menuid:self.menu_id block:^(NSDictionary *param){
                        
                        BOOL flag =  [[param valueForKey:@"flag"] boolValue];
                        if( flag ){
//                            [HCommon dismissMessage];
                            [HCommon popupSuccessMessage:@"上传成功"];
                            
                            [self.HunUploadVC updateUploadFlag];
                            
                            NSNotification * notice = [NSNotification notificationWithName:@"uploadSuccess" object:nil userInfo:nil];
                            [[NSNotificationCenter defaultCenter]postNotification:notice];
                        }else {
                            
                            NSString *msg = (NSString*)[param objectForKey: @"msg" ];
                            
                            [self alert:msg];
                            
                        }
                    }];

                

            }
                break;
            default:
                break;
        }
    }
}

- (void)NewHight:(UIButton *)sender
{
    HNightQualiatyViewController *HNightVC = [[HNightQualiatyViewController alloc]initWithMenuParam:self.param];
    HNightVC.title = @"新增夜查房";
    [self.navigationController pushViewController:HNightVC animated:YES];
}

@end
